package xyf.p10.dp;

/**
 * ClassName: P0063_Path2
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:26 AM
 * Version: 1.0
 **/
public class P0063_Path2
{
    public int uniquePathsWithObstacles(int[][] obstacleGrid)
    {
        int[][] dp = new int[obstacleGrid.length][obstacleGrid[0].length];

        for (int i = 0; i < obstacleGrid.length; i++)
        {
            if (obstacleGrid[i][0] == 1)
            {
                dp[i][0] = 0;
                continue;
            }
            if (i == 0)
            {
                dp[i][0] = 1;
            }
            else
            {
                dp[i][0] = dp[i - 1][0];
            }
        }

        for (int j = 0; j < obstacleGrid[0].length; j++)
        {
            if (obstacleGrid[0][j] == 1)
            {
                dp[0][j] = 0;
                continue;
            }
            if (j == 0)
            {
                dp[0][j] = 1;
            }
            else
            {
                dp[0][j] = dp[0][j - 1];
            }
        }

        for (int i = 1; i < obstacleGrid.length; i++)
        {
            for (int j = 1; j < obstacleGrid[0].length; j++)
            {
                if (obstacleGrid[i][j] == 1)
                {
                    dp[i][j] = 0;
                    continue;
                }
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }

        return dp[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
    }
}
